package com.dianqiu.common.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.dianqiu.base.action.BaseAction;
import com.dianqiu.common.bean.DqBetCompanyTO;
import com.dianqiu.common.bean.DqHandicapHistTO;
import com.dianqiu.common.bean.DqHandicapTO;
import com.dianqiu.common.bean.DqHandicapTypeTO;
import com.dianqiu.common.bean.DqMatchHistTO;
import com.dianqiu.common.bean.DqMatchTO;

public class ListHandicapAction extends BaseAction {
	/**
	 * 
	 */
	public String listHandicap(){
		//TODO
//		LoginUser loginUser = (LoginUser) this.getSession().get("loginUser");
//		Integer userId = Integer.parseInt(loginUser.getUserId());
		Integer userId = 1;
		String matchIdStr = this.getRequest().getParameter("matchId");
		
		//list handicap history according to matchId and userId
		List<DqHandicapHistTO> handicapHistList = this.getListHandicapService().getHandicapHistoryByUserIdAndMatchId(userId, Integer.parseInt(matchIdStr));
		this.getRequest().setAttribute("handicapHistList", handicapHistList);
		
		//list match history according to matchId
		List<DqMatchHistTO> matchHistoryList = this.getListHandicapService().getMatchHistoryById(Integer.parseInt(matchIdStr));
		if(matchHistoryList!=null && matchHistoryList.size()>0){
			DqMatchHistTO dqMatchHistTO = matchHistoryList.get(0);
			setMatchHistDateAndTime(dqMatchHistTO);
		}
		this.getRequest().setAttribute("matchHistoryList", matchHistoryList);
		
		return "success";
		
	}
	
	public String getHandicap(){
		
		String matchIdStr = this.getRequest().getParameter("matchId");
		List<DqHandicapTO> handicapList = this.getListHandicapService().getHandicapListByMatchId(Integer.parseInt(matchIdStr));
		this.getRequest().setAttribute("handicapList", handicapList);
		
		List<DqBetCompanyTO> dqBetCompanyList  = this.getListHandicapService().getAllDqBetCompany();
		
		List<DqHandicapTypeTO> dqHandicapTypeList = this.getListHandicapService().getAllDqHandicapType();
		
		this.getRequest().setAttribute("dqBetCompanyList", dqBetCompanyList);
		this.getRequest().setAttribute("dqHandicapTypeList", dqHandicapTypeList);
		this.getRequest().setAttribute("matchId", matchIdStr);
		
		return "add";
	}
	
	public String add(){
		HttpServletRequest request = this.getRequest();
		String betCompanyIdStr =  request.getParameter("BetCompanyId");
		String homeClbRateStr =  request.getParameter("HomeClbRate");
		String handicapTypeIdStr = request.getParameter("HandicapTypeId");
		String awayClbRateStr =  request.getParameter("AwayClbRate");
		String matchIdStr =  request.getParameter("MatchId");
		
		List<DqMatchTO> dqMatchTOList = this.getListHandicapService().getMatchById(Integer.valueOf(matchIdStr));
		
		List<DqBetCompanyTO> dqBetCompanyList = this.getListHandicapService().getBetCompanyByCompanyId(Integer.valueOf(betCompanyIdStr));
		
		List<DqHandicapTypeTO> dqHandicapTypeList = this.getListHandicapService().getHandicapTypeByTypeId(Integer.valueOf(handicapTypeIdStr));
		
		List<DqMatchHistTO> matchHistList = this.getListHandicapService().getMatchHistoryById(Integer.parseInt(matchIdStr));
		
		DqHandicapTO dqHandicapTO = new DqHandicapTO();
		
		dqHandicapTO.setAwayClbRate(Double.parseDouble(awayClbRateStr));
		dqHandicapTO.setHomeClbRate(Double.parseDouble(homeClbRateStr));
		
		dqHandicapTO.setDqHandicapType(dqHandicapTypeList.get(0));
		dqHandicapTO.setDqMatch(dqMatchTOList.get(0));
		dqHandicapTO.setDqBetCompany(dqBetCompanyList.get(0));
		dqHandicapTO.setUserId("1");
		
		this.createInitialize(dqHandicapTO, request);
		
		this.getListHandicapService().saveDqHandicap(dqHandicapTO);
		
		
		DqHandicapHistTO dqHandicapHistTO = new DqHandicapHistTO();
		
		dqHandicapHistTO.setHdpId(dqHandicapTO.getHdpId());
		dqHandicapHistTO.setAwayClbRate(Double.parseDouble(awayClbRateStr));
		dqHandicapHistTO.setHomeClbRate(Double.parseDouble(homeClbRateStr));
		
		dqHandicapHistTO.setDqHandicapType(dqHandicapTypeList.get(0));
		dqHandicapHistTO.setDqMatchHist(matchHistList.get(0));
		dqHandicapHistTO.setDqBetCompany(dqBetCompanyList.get(0));
		dqHandicapHistTO.setUserId("1");
		this.createInitialize(dqHandicapHistTO, request);
		
		this.getListHandicapService().saveDqHandicapHistoryTO(dqHandicapHistTO);
		
		List<DqHandicapTO> handicapList = this.getListHandicapService().getHandicapListByMatchId(Integer.parseInt(matchIdStr));
		this.getRequest().setAttribute("handicapList", handicapList);
		
		return "reload";
	}
	
	
	
}
